package com.commonlib.downloadmgr.ui;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;

import com.commonlib.downloadmgr.R;
import com.commonlib.downloadmgr.base.DownloadBaseJob;
import com.commonlib.downloadmgr.entity.Entity;
import com.commonlib.downloadmgr.subclass.DownloadAdapter;
import com.commonlib.downloadmgr.subclass.DownloadManager;


public class DownloadManagerActivity extends Activity implements OnClickListener, OnItemClickListener, OnItemLongClickListener{
//	private static final int UPDATE_INTERVAL = 1000;
//	/**
//     * Runnable periodically querying DownloadService about
//     * downloads
//     */
//    private Runnable mUpdateTimeTask = new Runnable() {
//            public void run() {
//            	mAdapter.notifyDataSetChanged();
//            	mHandler.postDelayed(this, UPDATE_INTERVAL);
//            }
//    };
	
//    private Handler mHandler;
    private ListView mListView;
    private DownloadAdapter mAdapter;
    
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.download);
        
//        mHandler = new Handler();
        mListView = (ListView)findViewById(R.id.lv_jobs);
        mAdapter = new DownloadAdapter(this.getApplicationContext());
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(this);
        mListView.setOnItemLongClickListener(this);
    }

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		mAdapter.notifyDataSetChanged();
		ArrayList<DownloadBaseJob<Entity>> allJobs = DownloadManager.getInstance(this).getAllJobs();
		for (DownloadBaseJob<Entity> job : allJobs) {
			job.setContainer(mListView);
		}
//		mHandler.postDelayed(mUpdateTimeTask, UPDATE_INTERVAL);
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		ArrayList<DownloadBaseJob<Entity>> allJobs = DownloadManager.getInstance(this).getAllJobs();
		for (DownloadBaseJob<Entity> job : allJobs) {
			job.setContainer(null);
		}
//		mHandler.removeCallbacks(mUpdateTimeTask);
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
	}

	@Override
	public boolean onItemLongClick(AdapterView<?> parent, View view,
			int position, long id) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		// TODO Auto-generated method stub
		
	}
    
}